/*
 * PConfiguracionFlujos.java
 *
 * Created on 20 de junio de 2006, 01:00 PM
 */

package ig.metrica.ui.confFlujos;

//import ig.metrica.ModeloPK;
import ig.metrica.ejb.conflujos.ConfiguracionflujosDTO;
import ig.metrica.ui.interfaces.comunes.IMetodosPantallas;
import ig.util.NoContinuaException;
import ig.util.TableModelConfFlujos;
import java.awt.Component;
import java.util.Collection;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;
//import org.apache.log4j.Logger;

/**
 *
 * @author  jsanchez
 */
public class PConfiguracionFlujos extends javax.swing.JPanel implements  IMetodosPantallas {
    
    private String[] nameColumnas={"Altura","Dias","%","Valor Mora","Saldo Capital"};
    private boolean[] editables={true,true,true,true,true};    
    Collection<ConfiguracionflujosDTO> CConfiguraciones=null;
    Collection CModelos=null;
    boolean init=false;
    private MConfiguracioFlujos modelo=null;
    /** Creates new form PConfiguracionFlujos */
    public PConfiguracionFlujos() throws NoContinuaException {
        initComponents();
//        CBoxModelos.setVisible(false);
        this.modelo=new MConfiguracioFlujos(this);
//        this.buscar();
        init=true;
    }
//    private static Logger log = Logger.getLogger(PConfiguracionFlujos.class.getName());
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        Tabla = new javax.swing.JTable();
        BNueva = new javax.swing.JButton();
        BEliminar = new javax.swing.JButton();

        Tabla.setModel(new TableModelConfFlujos(null,this.nameColumnas,this.editables));
        Tabla.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                TablaKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(Tabla);

        BNueva.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
        BNueva.setText(org.openide.util.NbBundle.getMessage(PConfiguracionFlujos.class, "PConfiguracionFlujos.BNueva.text")); // NOI18N
        BNueva.setToolTipText(org.openide.util.NbBundle.getMessage(PConfiguracionFlujos.class, "PConfiguracionFlujos.BNueva.toolTipText")); // NOI18N
        BNueva.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 10, 1, 10));
        BNueva.setOpaque(false);
        BNueva.setSelected(true);
        BNueva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BNuevaActionPerformed(evt);
            }
        });

        BEliminar.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
        BEliminar.setText(org.openide.util.NbBundle.getMessage(PConfiguracionFlujos.class, "PConfiguracionFlujos.BEliminar.text")); // NOI18N
        BEliminar.setToolTipText(org.openide.util.NbBundle.getMessage(PConfiguracionFlujos.class, "PConfiguracionFlujos.BEliminar.toolTipText")); // NOI18N
        BEliminar.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 10, 1, 10));
        BEliminar.setOpaque(false);
        BEliminar.setSelected(true);
        BEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BEliminarActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, BNueva, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, BEliminar, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(BNueva, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(BEliminar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void BEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BEliminarActionPerformed
            try {
                this.eliminar();
            } catch (Exception e) {
//                log.error(e.getMessage());
                e.printStackTrace();
            }        
    }//GEN-LAST:event_BEliminarActionPerformed

    private void TablaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TablaKeyReleased
        if(evt.getKeyCode()==127){
            try {
                this.eliminar();
            } catch (Exception e) {
//                log.error(e.getMessage());
                e.printStackTrace();
            }
        }
    }//GEN-LAST:event_TablaKeyReleased

    
    private void BNuevaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BNuevaActionPerformed
        try {
         this.nuevo();   
        } catch (Exception e) {
//            log.error(e.getMessage());
            e.printStackTrace();
        }
    }//GEN-LAST:event_BNuevaActionPerformed


    
            
    public JInternalFrame getInternalFrame() {
        return null;
    }

    public void validar() throws NoContinuaException {
    }

    public void guardar() throws NoContinuaException {
        modelo.guardar();
        init=false;
        this.buscar();
        init=true;
    }

    public void buscar() throws NoContinuaException {        
        this.modelo.buscar();
        Object[][] Data= null;
        if(CConfiguraciones!=null && CConfiguraciones.size()>0){
            Data= new Object[CConfiguraciones.size()][5];
            int i=0;
            for (ConfiguracionflujosDTO DTO: this.CConfiguraciones){
                Data[i][0]=DTO.getAltura();
                Data[i][1]=DTO.getDuracion();
                Data[i][2]=DTO.getProcentaje();
                if(DTO.getValormora()==1)Data[i][3]=new Boolean(true);else Data[i][3]=new Boolean(false);
                if(DTO.getSaldocapital()==1)Data[i][4]=new Boolean(true);else Data[i][4]=new Boolean(false);
                i++;
            }
        }
        this.setData(Data);
    }

    public void modificar() throws NoContinuaException {
    }

    public synchronized void nuevo() throws NoContinuaException {
        modelo.nuevo();        
    }
    public Object[][] getData(){
        return ((TableModelConfFlujos)Tabla.getModel()).getData();
    }    
    public void setData(Object[][] data){
        ((TableModelConfFlujos)Tabla.getModel()).setData(data);
        Tabla.getTableHeader().setReorderingAllowed(false);
        Tabla.updateUI();                       
    }
    
    public int getFilaSeleccionada(){
        return Tabla.getSelectedRow();
    }
    public void eliminar() throws NoContinuaException {
        if(this.getFilaSeleccionada()>-1 && this.getFilaSeleccionada()<Tabla.getRowCount()){
            if(JOptionPane.showConfirmDialog(null,"Elimiar la fila seleccionada","Mensaje",JOptionPane.OK_CANCEL_OPTION)==0)
                modelo.eliminar();
        }else
            JOptionPane.showMessageDialog(null,"No se ha seleccionado ninguna fila","Mensaje",JOptionPane.OK_OPTION);

    }

    public void salir() throws NoContinuaException {
        
    }

    public void imprimir() throws NoContinuaException {
    }

    public void cancelar() throws NoContinuaException {
    }

    public Component[] getControles() {
        return null;
    }

    public void setFrame(JFrame frame) {
    }

    public JFrame getFrame() {
        return null;
    }
    void setCConfiguraciones(Collection collection) {
        this.CConfiguraciones=collection;
    }
    void setCModelos(Collection collection) {
        this.CModelos=collection;
    }    
    
    public static void main(String[] arg){
        JDialog d= new JDialog();
        d.setSize(500,500);
        try {
            d.add(new PConfiguracionFlujos());
        } catch (NoContinuaException ex) {
            ex.printStackTrace();
        }
        d.setVisible(true);
    }
    private String nombreModelo=null;
    public void setNombreModelo(String string) throws NoContinuaException {
        this.nombreModelo=string;
        this.buscar();
//        this.CBoxModelos.setSelectedItem(string.trim());
//        this.CBoxModelos.setEnabled(false);
//        this.actionCmbo();
//        this.CBoxModelos.setVisible(false);
    }
    
    public void setEnabledAll(boolean b) {        
        this.BEliminar.setEnabled(b);
        this.BNueva.setEnabled(b);        
        ((javax.swing.JViewport)this.jScrollPane1.getComponents()[0]).getComponents()[0].setEnabled(b);
    }    

    public String getNombreModelo() {
        return nombreModelo;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BEliminar;
    private javax.swing.JButton BNueva;
    public javax.swing.JTable Tabla;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables


    @Override
    public Object getObjectToXML() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void setObjectToXML(Object object) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String getExtencionExportacion() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String getNombreExportacion() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}
